home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / array2.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  6.4 KB  |  373 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class ARRAY2[E]
  5.    --
  6.    -- Implementation of COLLECTION[E] based on ARRAY[E].
  7.    --
  8.  
  9. inherit COLLECTION2[E] redefine copy end;
  10.    
  11. creation make, array2
  12.  
  13. feature {ARRAY2}
  14.    
  15.    storage: ARRAY[ARRAY[E]];
  16.    
  17. feature 
  18.    
  19.    make(l1, u1, l2, u2: INTEGER) is
  20.      -- Reset all bounds `lower1' / `upper1' / `lower2' and
  21.      -- `upper2' using arguments as new values.
  22.      -- All elements are set to the default value of type E.
  23.       require
  24.      l1 <= u1;
  25.      l2 <= u2;
  26.       local
  27.      i: INTEGER;
  28.      sa: ARRAY[like item];
  29.       do
  30.      if storage /= Void then
  31.         free_storage(storage);
  32.      end;
  33.      from
  34.         !!storage.make(l1,u1);
  35.         i := storage.lower;
  36.      until
  37.         i > storage.upper
  38.      loop
  39.         !!sa.make(l2,u2);
  40.         storage.put(sa,i);
  41.         i := i + 1;
  42.      end;
  43.       ensure
  44.      lower1 = l1;
  45.      upper1 = u1;
  46.      lower2 = l2;
  47.      upper2 = u2
  48.       end;
  49.    
  50.    array2(s: ARRAY[ARRAY[E]]) is
  51.      -- The array `s' is used as internal memory of Current.
  52.      -- Assume all sub-arrays have the same indexing.
  53.      -- Also assume `s' is not shared with another object.
  54.       require
  55.      s /= Void;
  56.      s.count > 0;
  57.       local
  58.      i: INTEGER;
  59.       do
  60.      if storage /= Void then
  61.         free_storage(storage);
  62.      end
  63.      storage := s;
  64.      debug
  65.         from  
  66.            i := storage.lower + 1;
  67.         until
  68.            i > storage.upper
  69.         loop
  70.            if lower2 /= storage.item(i).lower then
  71.           std_error.put_string("ARRAY2.array2: Bad `lower2' at line ")
  72.           std_error.put_integer(i);
  73.           std_error.put_new_line;
  74.           crash;
  75.            elseif upper2 /= storage.item(i).upper then
  76.           std_error.put_string("ARRAY2.array2: Bad `upper2' at line ")
  77.           std_error.put_integer(i);
  78.           std_error.put_new_line;
  79.           crash;
  80.            end;
  81.            i := i + 1;
  82.         end;
  83.      end;
  84.       ensure
  85.      storage = s;
  86.       end;
  87.    
  88. feature 
  89.    
  90.    lower1: INTEGER is
  91.       do
  92.      Result := storage.lower;
  93.       end;
  94.    
  95.    upper1: INTEGER is
  96.       do
  97.      Result := storage.upper;
  98.       end;
  99.    
  100.    count1: INTEGER is
  101.       do
  102.      Result := storage.count;
  103.       end;
  104.    
  105.    lower2: INTEGER is
  106.       do
  107.      Result := storage.first.lower;
  108.       end;
  109.    
  110.    upper2: INTEGER is
  111.       do
  112.      Result := storage.first.upper;
  113.       end;
  114.    
  115.    count2: INTEGER is
  116.       do
  117.      Result := storage.first.count;
  118.       end;
  119.    
  120.    item(i, j: INTEGER): E is
  121.       do
  122.      Result := storage.item(i).item(j);
  123.       end;
  124.    
  125. feature 
  126.    
  127.    put(x: E; i, j: INTEGER) is
  128.       do
  129.      storage.item(i).put(x,j);     
  130.       end;
  131.    
  132.    copy(other: like Current) is
  133.       local
  134.      i: INTEGER;
  135.       do
  136.      make(other.lower1,other.upper1,other.lower2,other.upper2);
  137.      from  
  138.         i := other.lower1;
  139.         i := storage.lower;
  140.      until
  141.         i > storage.upper
  142.      loop
  143.         storage.item(i).copy(other.storage.item(i));
  144.         i := i + 1;
  145.      end;
  146.       end;
  147.    
  148. feature 
  149.  
  150.    set_all_with(x: E) is
  151.       local 
  152.          i: INTEGER;
  153.       do
  154.      from 
  155.         i := storage.lower
  156.      until
  157.         i > storage.upper
  158.          loop
  159.             storage.item(i).set_all_with(x)
  160.         i := i + 1
  161.          end;
  162.       end;
  163.  
  164.    nb_occurrences(elt: E): INTEGER is
  165.      -- Number of occurrences using `equal'.
  166.      -- See also `fast_nb_occurrences' to chose
  167.      -- the apropriate one.
  168.       local
  169.      i: INTEGER;
  170.       do
  171.      from  
  172.         i := lower1;
  173.      until
  174.         i > upper1
  175.      loop
  176.         Result := Result + storage.item(i).nb_occurrences(elt);
  177.         i := i + 1;
  178.      end;
  179.       ensure
  180.      Result >= 0;
  181.       end;
  182.       
  183.    fast_nb_occurrences(elt: E): INTEGER is
  184.      -- Number of occurrences using `='.
  185.       local
  186.      i: INTEGER;
  187.       do
  188.      from  
  189.         i := lower1;
  190.      until
  191.         i > upper1
  192.      loop
  193.         Result := Result + storage.item(i).fast_nb_occurrences(elt);
  194.         i := i + 1;
  195.      end;
  196.       ensure
  197.      Result >= 0;
  198.       end;
  199.  
  200. feature -- Resizing :
  201.  
  202.    resize(l1, u1, l2, u2: INTEGER) is
  203.       require
  204.      u1 >= l1 - 1;
  205.      u2 >= l2 - 1;
  206.       local
  207.      i: INTEGER;
  208.      oldl1, oldu1: INTEGER;
  209.      tmp: ARRAY[like item];
  210.       do
  211.      oldl1 := lower1;
  212.      oldu1 := upper1;
  213.      storage.resize (l1,u1); 
  214.      from
  215.         i := l1;
  216.      until
  217.         i > u1
  218.      loop
  219.         if i < oldl1 or i > oldu1 then
  220.            !!tmp.make (lower2, upper2);
  221.            storage.put (tmp, i)
  222.         else
  223.            storage.item (i).resize (l2,u2);
  224.         end;
  225.         i := i + 1;
  226.      end;
  227.       ensure
  228.      lower1 = l1;
  229.      upper1 = u1;
  230.      lower2 = l2;
  231.      upper2 = u2;
  232.       end;
  233.  
  234.    resize1(l, u: INTEGER) is
  235.       require
  236.      u >= l - 1;
  237.       local
  238.      i, oldl, oldc: INTEGER;
  239.      tmp: ARRAY[like item];
  240.       do
  241.      oldl := lower1;
  242.      oldc := upper1;
  243.      storage.resize (l,u);
  244.      if l < oldl then
  245.         from
  246.            i := l;
  247.         until
  248.            i = lower1
  249.         loop
  250.            !!tmp.make (lower2, upper2);
  251.            storage.put (tmp, i)
  252.            i := i + 1;
  253.         end;
  254.      end;
  255.      if u > oldc then
  256.         from
  257.            i := oldc + 1;
  258.         until
  259.            i > u
  260.         loop
  261.            !!tmp.make (lower2, upper2);
  262.            storage.put (tmp, i)
  263.            i := i + 1;
  264.         end;
  265.      end;
  266.       end;
  267.    
  268.    resize2 (l, u : INTEGER) is
  269.       require
  270.      u > l - 1;
  271.       local
  272.      i : INTEGER;
  273.       do
  274.      from
  275.         i := lower1;
  276.      until
  277.         i > upper1
  278.      loop
  279.         storage.item (i).resize (l,u);
  280.         i := i + 1;
  281.      end;
  282.       ensure
  283.      lower2 = l;
  284.      upper2 = u;
  285.       end;
  286.  
  287. feature -- Other features :
  288.       
  289.    replace_all(x, r: like item) is
  290.       local
  291.      i: INTEGER;
  292.       do
  293.      from 
  294.         i := lower1;
  295.      until
  296.         i > upper1
  297.      loop
  298.         storage.item(i).replace_all(x,r);
  299.         i := i + 1;
  300.      end;
  301.       end;
  302.    
  303.    fast_replace_all(x, r: like item) is
  304.       local
  305.      i: INTEGER;
  306.       do
  307.      from 
  308.         i := lower1;
  309.      until
  310.         i > upper1
  311.      loop
  312.         storage.item(i).fast_replace_all(x,r);
  313.         i := i + 1;
  314.      end;
  315.       end;
  316.      
  317.    transpose is
  318.       local
  319.      i,j : INTEGER;
  320.      oldc1, oldc2 : INTEGER;
  321.       do
  322.      oldc1 := count1;
  323.      oldc2 := count2;
  324.      if count1 > count2 then
  325.         resize2 (lower2, lower2 + count1 -1);
  326.      elseif count2 > count1 then
  327.         resize1 (lower1, lower1 + count2 - 1);
  328.      end;
  329.      from  
  330.         i := lower1;
  331.      until 
  332.         i > upper1 - 1
  333.      loop
  334.         from  
  335.            j := i + 1;
  336.         until 
  337.            j > upper2
  338.         loop
  339.            swap(i,j,j,i);
  340.            j := j + 1;
  341.         end;
  342.         i := i + 1;
  343.      end;
  344.      resize(lower1,lower1 + oldc2 - 1,lower2,lower2 + oldc1 - 1);
  345.       ensure
  346.      count = oldc1 * oldc2;     
  347.       end;
  348.  
  349. feature {NONE} 
  350.  
  351.    free_storage(s: like storage) is
  352.       require
  353.      s /= Void
  354.       local
  355.      i: INTEGER;
  356.       do
  357.      from
  358.         i := s.lower;
  359.      until
  360.         i > s.upper
  361.      loop
  362.         s.item(i).free;
  363.         i := i + 1;
  364.      end;
  365.      s.free;
  366.       end;
  367.  
  368. invariant
  369.    
  370.    storage.count >= 1;
  371.    
  372. end -- ARRAY2
  373.